#pragma strict
////////////////////////////////////////////////////////////////////////////////////
///射线段.
////////////////////////////////////////////////////////////////////////////////////
var line:LineRenderer;
//顶点数量
var vertexCount:int = 20;
//线长度
var length:float = 10; 
//材质
var mat:Material;
//alpha
var alpha:float = 1;
//渐变消失的时间.为0,不逐渐消失.
var lifeTime:float = 0;
//渐变的速度.
private var speed:float;

function Start () {
	line = GetComponent.<LineRenderer>();
	if(! line){
		line = gameObject.AddComponent.<LineRenderer>();
	}
	line.castShadows = false;
	line.receiveShadows = false;
	line.material = mat;
}

function Update () {
	if(alpha > 0){
		Show();
		AlphaDisappear();
	}else{
		Unshow();
	}
}

function StartShow(){
	alpha = 1;
}

private function Show(){
	line.material.SetFloat("_Alpha",alpha);
	line.SetVertexCount(vertexCount);
	var basePos:Vector3 = transform.position;
	if(vertexCount == 0)
		vertexCount = 2;
	var part:float = length/vertexCount;
	for(var i:int = 0;i<vertexCount;i++){
		var pos:Vector3 = basePos + transform.forward * part * i;
		line.SetPosition(i,pos);
	}
}
private function Unshow(){
	line.SetVertexCount(0);
}

private function AlphaDisappear(){
	if(lifeTime > 0){
		speed = 1.0 / lifeTime;
		alpha -= speed * Time.deltaTime;
	}
}